<?php

namespace App\Admin\Controllers;

use App\Models\UserBalance;
use App\Models\UserBalanceFlow;
use App\Models\User;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class UserBalanceFlowController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new UserBalanceFlow(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('user_id');
            $grid->column('type');
            $grid->column('from');
            $grid->column('balance');
            $grid->column('operator');
            $grid->column('comment');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new UserBalanceFlow(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('type');
            $show->field('from');
            $show->field('balance');
            $show->field('operator');
            $show->field('comment');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new UserBalanceFlow(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->text('type');
            $form->text('from');
            $form->text('balance');
            $form->text('operator');
            $form->text('comment');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }

    public function balanceFlow(Content $content, Request $request)
    {
        $userId = $request->input('user_id');
        $user = User::find($userId);
        $flow = $user->balanceFlow;
        $balance = UserBalance::where('user_id', $userId)->first();

        $content->title('消费记录');
        $content->description('');
        return $content->body(admin_view('balance_flow', compact('user', 'flow', 'balance')));
    }

    /**
     * @param Content $content
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function deleteFlow(Content $content, Request $request)
    {
        $flowId = $request->input('id');
        $userId = $request->input('user_id');
        DB::beginTransaction();
        try {

            $flow = UserBalanceFlow::find($flowId);
            // 回滚余额
            $balance = UserBalance::where('user_id', $userId)
                ->get()->first();

            if ($flow->type == 1) {
                // 充值
                $balance->balance += $flow->balance;
            } else {
                // 消费
                $balance->balance -= $flow->balance;
            }

            $balance->save();

            $flow->delete();

            DB::commit();
            $content->withSuccess('消费记录删除成功');
            return redirect()->back();

        } catch (\Exception $e) {
            DB::rollBack();

            $content->withError($e->getMessage());
            return redirect()->back();
        }

    }

}
